package cn.com.gridinfo.sooc.core.expand.score.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.jeremy.arad.Arad;
import com.jeremy.arad.utils.AnimUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.expand.score.common.CodeConst;
import cn.com.gridinfo.sooc.core.expand.score.dao.ScoreDao;
import cn.com.gridinfo.sooc.core.expand.score.fragment.CountsFragment;
import cn.com.gridinfo.sooc.core.expand.score.fragment.TxInfoFragment;
import cn.com.gridinfo.sooc.utils.IntentUtil;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 我的积分界面
 *
 * @author yingchunli
 */
public class MyCountsActivity extends BaseSoocActivity {

    @InjectView(R.id.toolbar)
    Toolbar toolbar;//toolbar
    @InjectView(R.id.toolbar_righttv)
    TextView toolbarRightText;//tooltbar右侧文字
    @InjectView(R.id.counts_info)
    RadioButton rbCountsInfo;//积分明细按钮
    @InjectView(R.id.jifen_text)
    TextView myScore;//我的积分
    @InjectView(R.id.radio_group)
    RadioGroup radioGroup;//积分明细和提现记录的标签容器
    private FragmentManager fm;//
    ScoreDao dao;
    CountsFragment countsFragment = new CountsFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expand_score_mycounts);
        ButterKnife.inject(this);
        dao = new ScoreDao(this);
        toolbarRightText.setVisibility(View.VISIBLE);//显示tooolbar右侧文字
        toolbarRightText.setText("我的邀请");//设置文字

        //获取我的积分
        dao.getMyCounts(CodeConst.GET_MYCOUNTS);
        //设置toolbar背景
        String release = Build.VERSION.RELEASE;  // android系统版本号
        if (release.startsWith("4")) {
            //5.0以下的系统
            toolbar.setBackgroundResource(R.mipmap.expand_myscore_toolbar_bg_small);
        } else {
            toolbar.setBackgroundResource(R.mipmap.expand_myscore_toolbar_bg);
        }

        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.content_counts, countsFragment);
        transaction.addToBackStack(null);
        transaction.commitAllowingStateLoss();
        rbCountsInfo.setChecked(true);

        //积分明细和提现记录的切换
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction transaction = fm.beginTransaction();
                switch (checkedId){
                    case R.id.counts_info://积分明细选中
                        transaction.replace(R.id.content_counts,  new CountsFragment());
                        break;

                    case R.id.tx_records://提现记录选中
                        transaction.replace(R.id.content_counts, new TxInfoFragment());
                        break;
                }
                transaction.addToBackStack(null);
                transaction.commitAllowingStateLoss();
            }
        });


    }

    /**
     * 提现的点击
     */
    @OnClick(R.id.tixian_btn)
    void onTxClick() {
        //跳转到提现页面
        IntentUtil.intentSlidIn(this, TxInfoActivity.class, 00);
    }

    /**
     * 赚积分的点击
     */
    @OnClick(R.id.zhuanjifen_btn)
    void onGetScoreClick() {
        //跳转到赚钱积分界面
        IntentUtil.intentSlidIn(this, GetScoreActivity.class, 00);
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.getStatus().equals("0")) {
                if (requestCode == CodeConst.GET_MYCOUNTS) {
                    if (dao.getStatus().equals("0")) {
                        //成功获取我的积分
                        myScore.setText(Arad.preferences.getString("score"));
                    }
                }
            } else {
                MessageUtils.showImageShortToast(this, dao.getStatusCode());
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "请求数据异常");
        }
    }

    @Override
    public String setupToolBarTitle() {
        return "我的积分";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    @OnClick(R.id.toolbar_leftbtn)
    void onToolbarClick() {
        finish();
    }

    @Override
    public boolean setToolbarRighttv(TextView toolbarRighttv) {
        return true;
    }

    /**
     * 我的邀请点击
     */
    @OnClick(R.id.toolbar_righttv)
    void onToolbarRightClick() {
        //跳转到我的邀请界面
        startActivity(new Intent(this, MyInviteActivity.class));
        AnimUtil.intentSlidIn(this);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        //从提现详情页面返回，重置我的积分
        dao.getMyCounts(CodeConst.GET_MYCOUNTS);
    }
}
